package com.rpg.world;


import com.rpg.Drawable_I;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Sprite implements Drawable_I {

	final Bitmap bmap;

	private Paint p;
	private int myX, myY;

	public Sprite(Bitmap b, int x, int y) {
		p = new Paint();
		bmap = b;
		myX = x;
		myY = y;
	}
	
	public int getX() { return myX + bmap.getWidth() / 2; }
	public int getY() { return myY + bmap.getHeight() / 2; }
	public void changeX(int x) { myX += x; }
	public void changeY(int y) { myY += y; }
	public void setX(int x) { myX = x - bmap.getWidth() / 2; }
	public void setY(int y) { myY = y - bmap.getHeight() / 2; }

	public Rect getBoundsPlus(int margin) {
		return new Rect(
				myX - margin, 
				myY - margin, 
				myX + bmap.getWidth() + margin,
				myY + bmap.getHeight() + margin);
	}
	
	public boolean overlaps(Sprite other) {
		return !(
				// they don't overlap if...
				// I am above other
				myY + bmap.getHeight() < other.myY || 
				// I am below other
				myY > other.myY + other.bmap.getHeight() ||
				// I am left of other
				myX + bmap.getWidth() < other.myX || 
				// I am right of other
				myX > other.myX + other.bmap.getWidth() 
			);
	}
	
	public void onDraw(Canvas canvas) {
		canvas.drawBitmap(bmap, myX, myY, p);
	}

}
